﻿// -------------------------------------------------------------------------
//    @FileName         :    NFServiceModule.cpp
//    @Author           :    Gao.Yi
//    @Date             :    2025-09-08
//    @Email			:    445267987@qq.com
//    @Module           :    NFServiceModule
//    @Desc             :
//
// -------------------------------------------------------------------------

#include "NFServiceModule.h"

#include "NFPacketSendUtil.h"
#include "NFServerLevelMgr.h"
#include "NFSession.h"
#include "proto_cs.pb.h"
#include "proto_cs_cmd.nanopb.h"

NFServiceModule::NFServiceModule(NFIPluginManager* p):NFLogicDynamicModule(p)
{
}

NFServiceModule::~NFServiceModule()
{
}

int NFServiceModule::Awake()
{
    RegisterClientMessage(NF_ST_LOGIC_SERVER, E_CS_GET_OPEN_SERVER_DAYS_REQ);
    RegisterServerMessage(NF_ST_LOGIC_SERVER, E_CS_SYSMSG_NTF);
    return 0;
}

int NFServiceModule::OnHandleClientMessage(NFDataPackage& packet, NFSession* pSession, const proto_ff::Proto_CSReq& req)
{
    CHECK_NULL(0, pSession);
    if (!m_pObjPluginManager->IsInited(NF_ST_LOGIC_SERVER))
    {
        NFLogError(NF_LOG_DEFAULT, packet.nParam1, "Logic Server not inited, drop client msg:{}", packet.ToString());
        return -1;
    }

    if (m_pObjPluginManager->IsServerStopping())
    {
        NFLogError(NF_LOG_DEFAULT, packet.nParam1, "Logic Server is Stopping, drop client msg:{}", packet.ToString());
        return ERR_CODE_SERVER_IS_STOPPING;
    }

    int iRetCode = 0;
    switch (packet.nMsgId)
    {
        case E_CS_GET_OPEN_SERVER_DAYS_REQ:
        {
            iRetCode = OnHandleGetOpenServerDaysReq(packet, pSession, req);
            break;
        }
        default:
        {
            iRetCode = -1;
            NFLogError(NF_LOG_DEFAULT, 0, "not handle msg:{}", packet.nMsgId);
        }
    }

    return iRetCode;
}

int NFServiceModule::OnHandleServerMessage(uint32_t msgId, NFDataPackage& packet, uint64_t param1, uint64_t param2)
{
    if (!m_pObjPluginManager->IsInited(NF_ST_LOGIC_SERVER))
    {
        NFLogError(NF_LOG_DEFAULT, packet.nParam1, "Logic Server not inited, drop client msg:{}", packet.ToString());
        return -1;
    }

    if (m_pObjPluginManager->IsServerStopping())
    {
        NFLogError(NF_LOG_DEFAULT, packet.nParam1, "Logic Server is Stopping, drop client msg:{}", packet.ToString());
        return ERR_CODE_SERVER_IS_STOPPING;
    }

    int iRetCode = 0;
    switch (packet.nMsgId)
    {
        case E_CS_SYSMSG_NTF:
        {
            iRetCode = OnHandleBroadSysMsgNtf(msgId, packet, param1, param2);
            break;
        }
        default: NFLogError(NF_LOG_DEFAULT, 0, "not handle msg:{}", packet.nMsgId);
    }

    return iRetCode;
}

int NFServiceModule::OnHandleBroadSysMsgNtf(uint32_t msgId, NFDataPackage& packet, uint64_t param1, uint64_t param2)
{
    proto_ff::Proto_CSRsp rsp;
    CLIENT_MSG_PROCESS_WITH_PRINTF(packet, rsp);
    NFPacketSendUtil::Broadcast(msgId, &rsp);
    return 0;
}

int NFServiceModule::OnHandleGetOpenServerDaysReq(NFDataPackage& packet, NFSession* pSession, const proto_ff::Proto_CSReq& csReq)
{
    CHECK_NULL(0, pSession);

    NFPlayer* pPlayer = pSession->GetPlayer();
    CHECK_NULL(0, pPlayer);

    proto_ff::Proto_CSRsp stRsp;
    proto_ff::Proto_CSGetOpenServerDaysRsp* pRsp = stRsp.mutable_get_open_server_days_rsp();

    pRsp->set_open_days(NFServerLevelMgr::Instance()->GetOpenDays());

    NFPacketSendUtil::SendToClient(pPlayer, E_CS_GET_OPEN_SERVER_DAYS_RSP, &stRsp);
    return 0;
}
